package logic

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"time"

	"MyBlogv2/Servers/WebSiteInfo/internal/svc"
	"MyBlogv2/Servers/WebSiteInfo/webSiteInfo"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendCodeOfRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendCodeOfRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendCodeOfRegisterLogic {
	return &SendCodeOfRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendCodeOfRegisterLogic) SendCodeOfRegister(in *webSiteInfo.WebSiteInfoRequest) (*webSiteInfo.SendCodeOfRegisterResponse, error) {
	email := in.Email
	typeOfCode := in.TypeOfCode

	if typeOfCode != "register" && typeOfCode != "reset" && typeOfCode != "resetEmail" {
		return &webSiteInfo.SendCodeOfRegisterResponse{
			Code: int64(HttpCode.PARAM_ERROR),
		}, nil
	}
	code := helper.GetRandNumber()
	err := helper.SendCode(email, code)
	if err != nil {
		return &webSiteInfo.SendCodeOfRegisterResponse{
			Code: int64(HttpCode.FAILURE),
		}, nil
	}
	models.RDb.Set(context.Background(), email, code, time.Minute*15)
	return &webSiteInfo.SendCodeOfRegisterResponse{
		Code: int64(HttpCode.SUCCESS),
	}, nil

}
